package com.taptech.doufu.weex.image;

import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.constant.Constant;

import java.io.File;
import java.lang.reflect.Field;

/**
 * Created by qlx on 2016/12/12.
 */

public class TFImageViewUtil {

    public static void setUrl(ImageView view , String url , float radiusRatio){
        if (url instanceof String){
            url = Constant.changeUrlHost(url);
            if(url.contains("tofu_local:")){
                int id = getResource(url.replace("tofu_local:",""));
                setRadiusImage(view, id, radiusRatio);
            }else{
                setRadiusImage(view, url, radiusRatio);
            }
        }
    }

    private static void setRadiusImage(ImageView view, Object url, float radiusRatio) {
        if (radiusRatio > 0 && radiusRatio<1){
            Glide.with(view.getContext()).load(url)
                    .transform(new GlideRoundTransform(view.getContext(),radiusRatio))
                    .dontAnimate()
                    .into(view);
        }else if(radiusRatio >= 1){
            Glide.with(view.getContext()).load(url)
                    .transform(new CenterCrop(),new com.taptech.doufu.util.GlideRoundTransform(view.getContext(),(int)radiusRatio))
                    .dontAnimate()
                    .into(view);
        }else {
            Glide.with(view.getContext()).load(url).dontAnimate().into(view);
        }
    }

    public static void setUrl(ImageView view , String url) {
        url = Constant.changeUrlHost(url);
        if(url.contains("tofu_local:")){
            int id = getResource(url.replace("tofu_local:","")
                    .replace(".png","").replace(".jpg",""));
            Glide.with(view.getContext()).load(id).dontAnimate().into(view);
        }else{
            Glide.with(view.getContext()).load(url).dontAnimate().into(view);
        }
    }


    public static int  getResource(String imageName){
        Class drawableClass = R.drawable.class;
        try {
            Field field = drawableClass.getDeclaredField(imageName);
            int resId = field.getInt(imageName);
            return resId;
        } catch (NoSuchFieldException e) {//如果没有在"drawable"下找到imageName,将会返回0
            return 0;
        } catch (IllegalAccessException e) {
            return 0;
        }
    }

    /**
     * 显示高斯模糊图片
     * @param view
     * @param url
     * @param vague
     */
    public static void showVagueImage(ImageView view, String url, int vague) {
        if (TextUtils.isEmpty(url)) {
            return;
        }
        url = Constant.changeUrlHost(url);
        if(url.contains("http")){
            Glide.with(WeMediaApplication.applicationContext)
                    .load(url)
                    .transform(new BlurTransformation(view.getContext(),vague,1))// “23”：设置模糊度(在0.0到25.0之间)，默认”25";"4":图片缩放比例,默认“1”。
                    .into(view);
        }else{
            Glide.with(WeMediaApplication.applicationContext)
                    .load(new File(url))
                    .transform(new BlurTransformation(view.getContext(),vague,1))// “23”：设置模糊度(在0.0到25.0之间)，默认”25";"4":图片缩放比例,默认“1”。
                    .into(view);
        }

    }
}
